from osgeo import ogr

coordinate01 = [
    [119.455914318822153,
     35.37769059631524],
    [119.462016582040206,
     35.377327576156731],
    [119.466076027528615,
     35.377135388352791],
    [119.467856945549357,
     35.377327576156731],
    [119.469559293657369,
     35.37726351360628],
    [119.473042559786137,
     35.37766924223348],
    [119.478280553964709,
     35.378544754951655],
    [119.478359123877382,
     35.37465826013117],
    [119.478476978746443,
     35.371358863510117],
    [119.478411503819217,
     35.369917337210417],
    [119.477219860143606,
     35.3670021720188],
    [119.455822653924017,
     35.367674911792307],
    [119.455914318822153,
     35.37769059631524]
]

coordinate02 = [
    [119.465146283561907,
     35.378032260854837],
    [119.470489037624048,
     35.382324298385875],
    [119.484003062604813,
     35.382409709750988],
    [119.484605431935336,
     35.364129616891056],
    [119.464962953765678,
     35.363916039936697],
    [119.465146283561907,
     35.378032260854837]
]


def change(coord):
    wkt = "POLYGON(("
    for coord in coordinate01:
        wkt = wkt + str(coord[0]) + " " + str(coord[1]) + ","
    wkt = wkt[:-1] + "))"
    return wkt


wkt1 = change(coordinate01)
wkt2 = change(coordinate02)

polygon1: ogr.Geometry = ogr.CreateGeometryFromWkt(wkt1)
polygon2: ogr.Geometry = ogr.CreateGeometryFromWkt(wkt2)

intersection: ogr.Geometry = polygon1.Intersection(polygon2)
union:ogr.Geometry = polygon1.Union(polygon2)
coordKML = intersection.ExportToKML()

head = '<?xml version="1.0" encoding="utf-8" ?><kml xmlns="http://www.opengis.net/kml/2.2"><Document id="root_doc"><Schema name="01" id="01"><SimpleField name="id" type="float"></SimpleField></Schema><Folder><name>01</name><Placemark><Style><LineStyle><color>#fff</color><width>4</width></LineStyle><PolyStyle><fill>1</fill><color>#DDA0DD</color></PolyStyle></Style><MultiGeometry>'
foot = '</MultiGeometry></Placemark></Folder></Document></kml>'

with open('./Include/dataset/kml.kml',"w") as f:
    f.write(head + coordKML + foot)
f.close()